package com.bloomfiler.demo.controller;

import com.alibaba.fastjson.JSON;
import com.bloomfiler.demo.dao.User;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

/**
 * 布隆过滤器实验
 */
public class RedisBloomFilterController {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RBloomFilter bloomFilter;

    private DefaultRedisScript<Long> script;

    @GetMapping("/add/{name}")
    public String addData(@PathVariable String name) {

        return bloomFilter.add(name) ? "ok" : "false";
    }

    @GetMapping("/list/{name}")
    public boolean listData(@PathVariable String name) throws InterruptedException {
        try {
            redissonClient.getLock("yy").tryLock(10, 2, TimeUnit.SECONDS);
        } catch (Exception e) {
            redissonClient.getLock("yy").unlock();
        }

        return bloomFilter.contains(name);

    }
}
